<?php

namespace App\Http\Controllers\Home\article;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class ArticleController extends Controller
{
    // add 发表文章
    public function add(Request $request)
    {
        // dd($request -> all());
         $data = DB::table('category')
            -> select('category.*')
            -> get();
        // dd($data);
        foreach ($data as $key => $value) {
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$key] -> catname = $str.$value -> catname;
        }
        // dd($data);
        return view('Home.article.add', ['data' => $data]);
    }

    // doadd 处理提交发表过来的文章
    public function doadd(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        // $data = $request -> except('_token');
        $time = date('Y-m-d H:i:s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        // dd($data);
        $res = DB::table('categoryinfo') -> insert($data);
        if($res)
        {
            return redirect('/') -> with(['info' => '发表成功']);
        }else
        {
            return back() -> with(['info' => '发表失败']);
        }
    }
}
